package com.example.module_home.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.moudle_house.module.HouseRepository
import com.bw.moudle_house.module.HouseResultData
import com.example.common.view.vm.BaseViewModell
import com.example.house.entity.HouseEntityItem
import com.example.lib_common.view.entity.NoticeEntity
import com.example.lib_common.view.model.BaseRepository
import com.example.lib_common.view.vm.BaseViewModel
import com.example.module_home.HomeActivity
import com.example.module_home.api.HouseApiService
import com.example.module_home.model.HomeRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * 协程调度器Dispatchers
 * Dispatchers.Main:在主线程中调度(使用线程池，其中线程数64个)，适用于耗时多任务场景
 * Dispatchers.IO:在子线程中调度(在Ui线程，只有1个)，适用于UI更新
 * Dispatchers.Default:取决于调用方（固定数量线程池，其中线程数CPU个数+1），适用于少量任务复杂计算
 * Dispatchers.Unconfined:没有限制(没有限制线程数量)
 *
 */
@HiltViewModel
class HouseViewModel1 @Inject constructor(private val apiService: HouseApiService):BaseViewModell<HouseRepository, HouseResultData<List<HouseEntityItem>>>(){
    fun getListData(regionId:Int){
        mViewModelScope.launch (mDispatcher){
            try {
                val result = apiService.getHouse(regionId)
                val res = HouseResultData(
                    data = result.data,
                    state = result.state,
                    msg = result.msg,
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败：${e.message}")
            }
        }
    }
    fun getListDataAll(){
        mViewModelScope.launch (mDispatcher){
            try {
                val result = apiService.getHouseAll()
                val res = HouseResultData(
                    data = result.data,
                    state = result.state,
                    msg = result.msg
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
}
